package day4.demo4_乐观锁;

import java.util.concurrent.atomic.AtomicStampedReference;

public class ABA {
    static AtomicStampedReference<Integer> money = new AtomicStampedReference<>(19,0);

    public static void main(String[] args) throws Exception{
        int version = money.getStamp();
        System.out.println("兑奖版本号："+version);
        new Thread(){
            @Override
            public void run() {
                Integer m = money.getReference();
                if(money.compareAndSet(m,39,version,version+1)){
                    System.out.println("兑奖成功"+money.getReference());
                }else {
                    System.out.println("你已经兑过了");
                }
            }
        }.start();
        Thread.sleep(1000);
        new Thread(){
            @Override
            public void run() {
                if(money.compareAndSet(39,19,1,2)){
                    System.out.println("消费20元，余额为："+money.getReference());
                }
            }
        }.start();
        Thread.sleep(1000);

        new Thread(){
            @Override
            public void run() {
                Integer m = money.getReference();
                if(money.compareAndSet(m,39,version,version+1)){
                    System.out.println("兑奖成功"+money.getReference());
                }else {
                    System.out.println("你已经兑过了");
                }
            }
        }.start();
    }


}
